package com.ecloud.support.generator.service.impl;

import java.time.LocalDateTime;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ecloud.common.base.result.Result;
import com.ecloud.common.base.result.ResultBuilder;
import com.ecloud.common.base.result.ResultStatus;
import com.ecloud.common.data.annotation.DynamicDS;
import com.ecloud.common.utils.code.UUIDUtil;
import com.ecloud.support.generator.mapper.TemplateSetMapper;
import com.ecloud.support.generator.model.convert.TemplateSetConverter;
import com.ecloud.support.generator.model.dto.TemplateSetDto;
import com.ecloud.support.generator.model.entity.TemplateSetPo;
import com.ecloud.support.generator.service.iface.IDbConfigService;
import com.ecloud.support.generator.service.iface.IDynamicService;

/**
 * DynamicService
 *
 * @author LiuGangQiang Create in 2021/07/17
 */
@Service
@DynamicDS
public class DynamicService extends ServiceImpl<TemplateSetMapper, TemplateSetPo> implements IDynamicService {
    @Resource
    private IDynamicService service;
    @Resource
    private IDbConfigService configService;

    /**
     * @see com.ecloud.support.generator.service.iface.IDynamicService#insert(com.ecloud.support.generator.model.dto.TemplateSetDto)
     */
    @Override
    @Transactional
    public Result<?> insert() {
        TemplateSetDto dto = new TemplateSetDto();
        service.insert1(dto);
        service.insert2(dto);
        service.insert3(dto);
        service.insert4(dto);
        service.insert5(dto);
        /* 随机抛出异常测试批量回滚 */

        if (System.currentTimeMillis() % 2 == 0) {
            throw new RuntimeException();
        }
        return ResultBuilder.build(ResultStatus.OK);
    }

    /**
     * @see com.ecloud.support.generator.service.iface.IDynamicService#insert1(com.ecloud.support.generator.model.dto.TemplateSetDto)
     */
    @Override
    @DynamicDS("db001")
    public Result<?> insert1(TemplateSetDto dto) {
        dto.setId(UUIDUtil.uuid());
        dto.setSetName("这条数据应该在DB001");
        dto.setDesc("这条数据应该在DB001");
        dto.setCreateTime(LocalDateTime.now());
        TemplateSetPo po = TemplateSetConverter.INSTANCE.dtoToPo(dto);
        if (this.save(po)) {
            return ResultBuilder.build(ResultStatus.OK);
        }
        return ResultBuilder.build(ResultStatus.FAIL);
    }

    /**
     * @see com.ecloud.support.generator.service.iface.IDynamicService#insert2(com.ecloud.support.generator.model.dto.TemplateSetDto)
     */
    @Override
    @DynamicDS("db002")
    public Result<?> insert2(TemplateSetDto dto) {
        dto.setId(UUIDUtil.uuid());
        dto.setSetName("这条数据应该在DB002");
        dto.setDesc("这条数据应该在DB002");
        dto.setCreateTime(LocalDateTime.now());
        TemplateSetPo po = TemplateSetConverter.INSTANCE.dtoToPo(dto);
        if (this.save(po)) {
            return ResultBuilder.build(ResultStatus.OK);
        }
        return ResultBuilder.build(ResultStatus.FAIL);
    }

    /**
     * @see com.ecloud.support.generator.service.iface.IDynamicService#insert3(com.ecloud.support.generator.model.dto.TemplateSetDto)
     */
    @Override
    @DynamicDS("db003")
    public Result<?> insert3(TemplateSetDto dto) {
        dto.setId(UUIDUtil.uuid());
        dto.setSetName("这条数据应该在DB003");
        dto.setDesc("这条数据应该在DB003");
        dto.setCreateTime(LocalDateTime.now());
        TemplateSetPo po = TemplateSetConverter.INSTANCE.dtoToPo(dto);
        if (this.save(po)) {
            return ResultBuilder.build(ResultStatus.OK);
        }
        return ResultBuilder.build(ResultStatus.FAIL);
    }

    /**
     * @see com.ecloud.support.generator.service.iface.IDynamicService#insert4(com.ecloud.support.generator.model.dto.TemplateSetDto)
     */
    @Override
    @DynamicDS("db004")
    public Result<?> insert4(TemplateSetDto dto) {
        dto.setId(UUIDUtil.uuid());
        dto.setSetName("这条数据应该在DB004");
        dto.setDesc("这条数据应该在DB004");
        dto.setCreateTime(LocalDateTime.now());
        TemplateSetPo po = TemplateSetConverter.INSTANCE.dtoToPo(dto);
        if (this.save(po)) {
            return ResultBuilder.build(ResultStatus.OK);
        }
        return ResultBuilder.build(ResultStatus.FAIL);
    }

    /**
     * @see com.ecloud.support.generator.service.iface.IDynamicService#insert5(com.ecloud.support.generator.model.dto.TemplateSetDto)
     */
    @Override
    public Result<?> insert5(TemplateSetDto dto) {
        dto.setId(UUIDUtil.uuid());
        dto.setSetName("这条数据应该在master");
        dto.setDesc("这条数据应该在master");
        dto.setCreateTime(LocalDateTime.now());
        TemplateSetPo po = TemplateSetConverter.INSTANCE.dtoToPo(dto);
        if (this.save(po)) {
            return ResultBuilder.build(ResultStatus.OK);
        }
        return ResultBuilder.build(ResultStatus.FAIL);
    }
}
